package com.rex.saas.utils;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;

public class EmailSenderQQ {
    private static volatile EmailSenderQQ instance;

    private Session session;
    
    private static final String SENDER_EMAIL = "547203131@qq.com";
    private static final String HOST = "smtp.qq.com";
    private static final String PASSWORD = "gfyopwcecrhhbbcd";

    private EmailSenderQQ() throws GeneralSecurityException {
        configureSession("SSL", "smtp");
    }

    public static EmailSenderQQ getInstance() throws GeneralSecurityException {
        if (instance == null) {
            synchronized (EmailSenderQQ.class) {
                if (instance == null) {
                    instance = new EmailSenderQQ();
                }
            }
        }
        return instance;
    }

    public void updateConfiguration(String encryptionType, String protocol) throws GeneralSecurityException {
        configureSession(encryptionType, protocol);
    }

    private void configureSession(String encryptionType, String protocol) throws GeneralSecurityException {
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol", protocol);
        properties.setProperty("mail.smtp.auth", "true");

        switch (encryptionType) {
            case "None":
                properties.setProperty("mail.smtp.ssl.enable", "false");
                properties.setProperty("mail.smtp.starttls.enable", "false");
                break;
            case "SSL":
                properties.setProperty("mail.smtp.ssl.enable", "true");
                properties.setProperty("mail.smtp.starttls.enable", "false");
                MailSSLSocketFactory sf = new MailSSLSocketFactory();
                sf.setTrustAllHosts(true);
                properties.put("mail.smtp.ssl.socketFactory", sf);
                break;
            case "TLS":
                properties.setProperty("mail.smtp.ssl.enable", "false");
                properties.setProperty("mail.smtp.starttls.enable", "true");
                break;
        }

        session = Session.getInstance(properties);
        session.setDebug(false);
    }

    public void sendEmail(String recipient, String subject, String content) throws MessagingException {
        Transport transport = session.getTransport();
        transport.connect("smtp.qq.com", "547203131@qq.com", "gfyopwcecrhhbbcd");
        MimeMessage mimeMessage = new MimeMessage(session);
        mimeMessage.setFrom(new InternetAddress("547203131@qq.com"));
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
        mimeMessage.setSubject(subject);
        mimeMessage.setContent(content, "text/html;charset=UTF-8");
        transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
        transport.close();
    }

    public static void main(String[] args) throws GeneralSecurityException, MessagingException {
        EmailSenderQQ emailSender = EmailSenderQQ.getInstance();
        emailSender.sendEmail("547203131@qq.com", "Test Email", "发送SSL邮箱.");

//        emailSender.updateConfiguration("TLS", "");
//        emailSender.sendEmail("547203131@qq.com", "Modified Test Email", "发送TLS邮箱.");
//        emailSender = EmailSenderUtils.getInstance();
//        emailSender.sendEmail("547203131@qq.com", "Modified Test Email", "发送TLS邮箱.");
    }
}